package br.com.gsc.vaadin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

import br.com.gsc.model.tableMapping.Admin;
import br.com.gsc.model.tableMapping.Company;
import br.com.gsc.model.tableMapping.Person;
import br.com.gsc.model.tableMapping.Product;
import br.com.gsc.repository.objRepos.CompanyRepository;
import br.com.gsc.repository.objRepos.PersonRepository;
import br.com.gsc.repository.objRepos.ProductRepository;
import br.com.gsc.setup.AuthenticationHolder;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.RichTextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

@Configurable(preConstruction=true)
public class AddProductWindow extends CustomComponent {

	@AutoGenerated
	private AbsoluteLayout mainLayout;
	@AutoGenerated
	private Panel panel_1;
	@AutoGenerated
	private VerticalLayout verticalLayout_2;
	@AutoGenerated
	private Panel panel_7;
	@AutoGenerated
	private GridLayout gridLayout_2;
	@AutoGenerated
	private Button button_1;
	@AutoGenerated
	private RichTextArea richTextArea_1;
	@AutoGenerated
	private Label label_6;
	@AutoGenerated
	private ComboBox comboBox_1;
	@AutoGenerated
	private Label label_5;
	@AutoGenerated
	private TextField textField_3;
	@AutoGenerated
	private Label label_4;
	@AutoGenerated
	private Panel panel_5;
	@AutoGenerated
	private VerticalLayout verticalLayout_6;
	@AutoGenerated
	private Label label_3;

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private CompanyRepository cr;
	@Autowired
	private ProductRepository pr;
	@Autowired
	private PersonRepository pRepo;	
	@Autowired
	private AuthenticationHolder auth;
	private Person p;
	
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public AddProductWindow() {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		setupAddButtonListener();
		p = pRepo.findPersonByID(auth.getAuthentication().getName());
		if(p instanceof Admin){
			List<Company> comp = ((Admin)p).getListOfCompanies();
			for(Company c : comp){
				comboBox_1.addItem(c);
			}
		}
		// TODO add user code here
	}
	
	public void setupAddButtonListener(){
		button_1.addListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				Company comp = new Company();								
				Product prod = new Product();
				prod.setCompany(comp);
				prod.setName(textField_3.getValue().toString());
				if(comboBox_1.getValue() != null){
					prod.setCompany((Company) comboBox_1.getValue());
				}
				pr.addProduct(prod);
				getWindow().showNotification("Produto Gravado!");
			}
		});
	}

	@AutoGenerated
	private AbsoluteLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new AbsoluteLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// panel_1
		panel_1 = buildPanel_1();
		mainLayout.addComponent(panel_1, "right:0.0px;bottom:0.0px;");
		
		return mainLayout;
	}

	@AutoGenerated
	private Panel buildPanel_1() {
		// common part: create layout
		panel_1 = new Panel();
		panel_1.setImmediate(false);
		panel_1.setWidth("100.0%");
		panel_1.setHeight("100.0%");
		
		// verticalLayout_2
		verticalLayout_2 = buildVerticalLayout_2();
		panel_1.setContent(verticalLayout_2);
		
		return panel_1;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_2() {
		// common part: create layout
		verticalLayout_2 = new VerticalLayout();
		verticalLayout_2.setImmediate(false);
		verticalLayout_2.setWidth("100.0%");
		verticalLayout_2.setHeight("100.0%");
		verticalLayout_2.setMargin(true);
		verticalLayout_2.setSpacing(true);
		
		// panel_5
		panel_5 = buildPanel_5();
		verticalLayout_2.addComponent(panel_5);
		
		// panel_7
		panel_7 = buildPanel_7();
		verticalLayout_2.addComponent(panel_7);
		verticalLayout_2.setExpandRatio(panel_7, 1.0f);
		
		return verticalLayout_2;
	}

	@AutoGenerated
	private Panel buildPanel_5() {
		// common part: create layout
		panel_5 = new Panel();
		panel_5.setImmediate(false);
		panel_5.setDescription("Cadastrar Empresas");
		panel_5.setWidth("100.0%");
		panel_5.setHeight("40px");
		
		// verticalLayout_6
		verticalLayout_6 = buildVerticalLayout_6();
		panel_5.setContent(verticalLayout_6);
		
		return panel_5;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_6() {
		// common part: create layout
		verticalLayout_6 = new VerticalLayout();
		verticalLayout_6.setImmediate(false);
		verticalLayout_6.setWidth("100.0%");
		verticalLayout_6.setHeight("100.0%");
		verticalLayout_6.setMargin(false);
		
		// label_3
		label_3 = new Label();
		label_3.setImmediate(false);
		label_3.setWidth("100.0%");
		label_3.setHeight("100.0%");
		label_3.setValue("Cadastrar Empresas");
		verticalLayout_6.addComponent(label_3);
		
		return verticalLayout_6;
	}

	@AutoGenerated
	private Panel buildPanel_7() {
		// common part: create layout
		panel_7 = new Panel();
		panel_7.setImmediate(false);
		panel_7.setWidth("100.0%");
		panel_7.setHeight("100.0%");
		
		// gridLayout_2
		gridLayout_2 = buildGridLayout_2();
		panel_7.setContent(gridLayout_2);
		
		return panel_7;
	}

	@AutoGenerated
	private GridLayout buildGridLayout_2() {
		// common part: create layout
		gridLayout_2 = new GridLayout();
		gridLayout_2.setImmediate(false);
		gridLayout_2.setWidth("100.0%");
		gridLayout_2.setHeight("100.0%");
		gridLayout_2.setMargin(true);
		gridLayout_2.setSpacing(true);
		gridLayout_2.setRows(10);
		
		// label_4
		label_4 = new Label();
		label_4.setImmediate(false);
		label_4.setWidth("-1px");
		label_4.setHeight("-1px");
		label_4.setValue("Nome do Produto");
		gridLayout_2.addComponent(label_4, 0, 0);
		
		// textField_3
		textField_3 = new TextField();
		textField_3.setImmediate(false);
		textField_3.setWidth("-1px");
		textField_3.setHeight("-1px");
		textField_3.setSecret(false);
		gridLayout_2.addComponent(textField_3, 0, 1);
		
		// label_5
		label_5 = new Label();
		label_5.setImmediate(false);
		label_5.setWidth("-1px");
		label_5.setHeight("-1px");
		label_5.setValue("Nome da Empresa");
		gridLayout_2.addComponent(label_5, 0, 2);
		
		// comboBox_1
		comboBox_1 = new ComboBox();
		comboBox_1.setImmediate(false);
		comboBox_1.setWidth("-1px");
		comboBox_1.setHeight("-1px");
		gridLayout_2.addComponent(comboBox_1, 0, 3);
		
		// label_6
		label_6 = new Label();
		label_6.setImmediate(false);
		label_6.setWidth("-1px");
		label_6.setHeight("-1px");
		label_6.setValue("Descrição do Produto");
		gridLayout_2.addComponent(label_6, 0, 4);
		
		// richTextArea_1
		richTextArea_1 = new RichTextArea();
		richTextArea_1.setImmediate(false);
		richTextArea_1.setWidth("-1px");
		richTextArea_1.setHeight("-1px");
		gridLayout_2.addComponent(richTextArea_1, 0, 5);
		
		// button_1
		button_1 = new Button();
		button_1.setCaption("Gravar Produto");
		button_1.setImmediate(true);
		button_1.setWidth("150px");
		button_1.setHeight("-1px");
		gridLayout_2.addComponent(button_1, 0, 6);
		gridLayout_2.setComponentAlignment(button_1, new Alignment(10));
		
		return gridLayout_2;
	}

}
